/*
 * @Author: jiejie
 * @Github: https://github.com/jiejieTop
 * @Date: 2019-12-23 19:26:27
 * @LastEditTime: 2020-04-25 08:44:24
 * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
 */
#include "platform_thread.h"
#include "platform_memory.h"

extern void utilsAssertFail(const char *cond, const char *file, signed short line, unsigned char allowDiag);
#define WARNING(cOND) { if (!(cOND)) { utilsAssertFail(NULL, __FILE__, __LINE__, 0xFF); } }
#define ASSERT(cOND) { if (!(cOND)) { utilsAssertFail(#cOND, __FILE__, __LINE__, 1); } }

platform_thread_t *platform_thread_init(char * name,
                                        void (*entry)(void *),
                                        void * param,
                                        unsigned int stack_size,
                                        unsigned int priority,
                                        unsigned int tick)
{
    platform_thread_t *thread;
    OSTaskRef TaskRef = NULL;
    void* ptr_task_stack = NULL;

    thread = platform_memory_alloc(sizeof(platform_thread_t));

    if(NULL == thread)
    {
        return NULL;
    }

    ptr_task_stack = (void*)malloc(stack_size);
    ASSERT(ptr_task_stack != NULL);	
	
    ASSERT(OS_SUCCESS == OSATaskCreate( &TaskRef,
                ptr_task_stack,
                stack_size,
                priority,
                name,
                entry,
                param));

    thread->thread = TaskRef;
    thread->th_stack = ptr_task_stack;

	/* coverity[leaked_storage] */
    return thread;    

}

void platform_thread_startup(platform_thread_t* thread)
{
    /*add print here*/
}


void platform_thread_stop(platform_thread_t* thread)
{
    OSATaskSuspend(thread->thread);
    
}

void platform_thread_start(platform_thread_t* thread)
{
    OSATaskResume(thread->thread);
}

void platform_thread_destroy(platform_thread_t* thread)
{
    OSA_STATUS status;
    status = OSATaskDelete(thread->thread);
    ASSERT(status == OS_SUCCESS);    

    platform_memory_free(thread->th_stack);
    platform_memory_free(thread);
}


